﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;

namespace Besture.UI.Control
{
    public partial class Pager : System.Web.UI.UserControl
    {
        /// <summary>
        /// 需要分页的RepeaterID
        /// </summary>
        public string RepeaterID { get; set; }
        /// <summary>
        /// 错误提示
        /// </summary>
        public string ErrorText { get; set; }
        /// <summary>
        /// 内容占位符
        /// </summary>
        public string ContentPlaceHolder { get; set; }

        #region 文字属性

        /// <summary>
        /// 首页
        /// </summary>
        public string FirstPageText { get; set; }
        /// <summary>
        /// 上一页
        /// </summary>
        public string PrevPageText { get; set; }
        /// <summary>
        /// 下一页
        /// </summary>
        public string NextPageText { get; set; }
        /// <summary>
        /// 尾页
        /// </summary>
        public string LastPageText { get; set; }

        /// <summary>
        /// 是否显示第一页和最后一页
        /// </summary>
        public bool ShowFirstLast { get; set; }

        #endregion

        #region 样式属性

        /// <summary>
        /// 当前页面样式
        /// </summary>
        public string CurrentPageButtonClass { get; set; }

        #endregion

        #region 功能属性

        /// <summary>
        /// 是否一直显示分页代码
        /// </summary>
        public bool AlaysShow { get; set; }
        /// <summary>
        /// 每页显示数目（默认为10条）
        /// </summary>
        public int PageSize { get; set; }

        #endregion

        /// <summary>
        /// 初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化控件属性
            InitPager();
            //绑定数据
            BindData();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        protected void InitPager()
        {
            if (string.IsNullOrEmpty(FirstPageText))
            {
                FirstPageText = "首页";
            }
            if (string.IsNullOrEmpty(PrevPageText))
            {
                PrevPageText = "上一页";
            }
            if (string.IsNullOrEmpty(NextPageText))
            {
                NextPageText = "下一页";
            }
            if (string.IsNullOrEmpty(LastPageText))
            {
                LastPageText = "尾页";
            }

            if (string.IsNullOrEmpty(CurrentPageButtonClass))
            {
                CurrentPageButtonClass = "selected";
            }
            //文字
            AspNetPager1.FirstPageText = FirstPageText;
            AspNetPager1.LastPageText = LastPageText;
            AspNetPager1.NextPageText = NextPageText;
            AspNetPager1.PrevPageText = PrevPageText;
            //样式
            AspNetPager1.CurrentPageButtonClass = CurrentPageButtonClass;
            //功能
            AspNetPager1.AlwaysShow = AlaysShow;
            AspNetPager1.ShowFirstLast = ShowFirstLast;

            if (PageSize == 0)
            {
                //默认每页显示10条
                PageSize = 10;
            }
            AspNetPager1.PageSize = PageSize;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void BindData()
        {
            if (string.IsNullOrEmpty(RepeaterID))
            {
                ErrorText = "未对分页控件设置RepeaterID,请检查！";
                return;
            }
            //判断是否存在母版
            string pagefile = Page.MasterPageFile;

            CmsControl.Repeater repeater = null;
            //不存在母版
            if(string.IsNullOrEmpty(pagefile))
            {
                repeater = (CmsControl.Repeater)Page.FindControl(RepeaterID);
            }
            else
            {
                //Cms默认页面的占位符ID
                if(string.IsNullOrEmpty(ContentPlaceHolder))
                {
                    ContentPlaceHolder = "ContentPlaceHolder";
                }
                repeater = (CmsControl.Repeater)Page.Master.FindControl(ContentPlaceHolder).FindControl(RepeaterID);
            }
            DataTable table = null;
            //数据源不为空
            if (!string.IsNullOrEmpty(repeater.DataName))
            {
                string dataName = repeater.DataName;
                table = QueryDac.GetSource(dataName);
            }
            else
            {
                //读取Sql
                string sql = repeater.Sql;
                table = QueryDac.GetTable(sql);
            }
            
            AspNetPager1.RecordCount = table.Rows.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.DataSource = table.DefaultView; //为控件绑定数据
            pds.CurrentPageIndex = 1;
            pds.PageSize = AspNetPager1.PageSize; //获取每页显示的数量
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            repeater.DataSource = pds;
            repeater.DataBind();
        }

        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(ErrorText))
            {
                writer.Write(ErrorText);
                return;
            }
            base.Render(writer);
        }
    }
}